package com.ruoyi.system.service.impl;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.LuntaiVideoLog;
import com.ruoyi.system.domain.dto.TyreInstallLogDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.LuntaiTyreInstallLogMapper;
import com.ruoyi.system.domain.LuntaiTyreInstallLog;
import com.ruoyi.system.service.ILuntaiTyreInstallLogService;

/**
 * 轮胎安装记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-13
 */
@Service
public class LuntaiTyreInstallLogServiceImpl implements ILuntaiTyreInstallLogService 
{
    @Autowired
    private LuntaiTyreInstallLogMapper luntaiTyreInstallLogMapper;

    /**
     * 查询轮胎安装记录
     * 
     * @param id 轮胎安装记录主键
     * @return 轮胎安装记录
     */
    @Override
    public LuntaiTyreInstallLog selectLuntaiTyreInstallLogById(Integer id)
    {
        return luntaiTyreInstallLogMapper.selectLuntaiTyreInstallLogById(id);
    }

    /**
     * 查询轮胎安装记录列表
     * 
     * @param luntaiTyreInstallLog 轮胎安装记录
     * @return 轮胎安装记录
     */
    @Override
    public List<LuntaiTyreInstallLog> selectLuntaiTyreInstallLogList(LuntaiTyreInstallLog luntaiTyreInstallLog)
    {
        return luntaiTyreInstallLogMapper.selectLuntaiTyreInstallLogList(luntaiTyreInstallLog);
    }

    /**
     * 新增轮胎安装记录
     * 
     * @param luntaiTyreInstallLog 轮胎安装记录
     * @return 结果
     */
    @Override
    public int insertLuntaiTyreInstallLog(LuntaiTyreInstallLog luntaiTyreInstallLog)
    {
        luntaiTyreInstallLog.setCreateTime(DateUtils.getNowDate());
        return luntaiTyreInstallLogMapper.insertLuntaiTyreInstallLog(luntaiTyreInstallLog);
    }

    /**
     * 修改轮胎安装记录
     * 
     * @param luntaiTyreInstallLog 轮胎安装记录
     * @return 结果
     */
    @Override
    public int updateLuntaiTyreInstallLog(LuntaiTyreInstallLog luntaiTyreInstallLog)
    {
        return luntaiTyreInstallLogMapper.updateLuntaiTyreInstallLog(luntaiTyreInstallLog);
    }

    /**
     * 批量删除轮胎安装记录
     * 
     * @param ids 需要删除的轮胎安装记录主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiTyreInstallLogByIds(Integer[] ids)
    {
        return luntaiTyreInstallLogMapper.deleteLuntaiTyreInstallLogByIds(ids);
    }

    /**
     * 删除轮胎安装记录信息
     * 
     * @param id 轮胎安装记录主键
     * @return 结果
     */
    @Override
    public int deleteLuntaiTyreInstallLogById(Integer id)
    {
        return luntaiTyreInstallLogMapper.deleteLuntaiTyreInstallLogById(id);
    }

    @Override
    public List<TyreInstallLogDto> getTyreListByCarId2(Integer carId) {
        List<TyreInstallLogDto> tyreListByCarId2 = luntaiTyreInstallLogMapper.getTyreListByCarId2(carId);
        for (TyreInstallLogDto tyreInstallLogDto : tyreListByCarId2) {
            if (tyreInstallLogDto.getInstallationTime() != null) {
                // 获取当前时间
                Calendar calendar = Calendar.getInstance();
                // 计算 1 年前的时间
                calendar.add(Calendar.YEAR, -1);
                Date oneYearAgo = calendar.getTime();
                // 如果安装时间早于 1 年前，设置 isReparation 为 2
                if (tyreInstallLogDto.getInstallationTime().before(oneYearAgo)) {
                    tyreInstallLogDto.setIsReparation(2);
                }
            }
        }
        return tyreListByCarId2;
    }

    @Override
    public List<TyreInstallLogDto> installLogByLicensePlate(String licensePlate) {
        List<TyreInstallLogDto> list = luntaiTyreInstallLogMapper.getInstallLogByLicensePlate(licensePlate);
        for (TyreInstallLogDto tyreInstallLogDto : list) {
            if (tyreInstallLogDto.getInstallationTime() != null) {
                // 获取当前时间
                Calendar calendar = Calendar.getInstance();
                // 计算 1 年前的时间
                calendar.add(Calendar.YEAR, -1);
                Date oneYearAgo = calendar.getTime();
                // 如果安装时间早于 1 年前，设置 isReparation 为 2
                if (tyreInstallLogDto.getInstallationTime().before(oneYearAgo)) {
                    tyreInstallLogDto.setIsReparation(2);
                }
            }
        }
        return list;
    }

    @Override
    public List<LuntaiVideoLog> getVideoList(LuntaiVideoLog videoLog) {
        return luntaiTyreInstallLogMapper.getVideoList(videoLog);
    }

    @Override
    public int deleteVideo(Integer id) {
        // 获取视频信息
        LuntaiVideoLog videoLog = luntaiTyreInstallLogMapper.getVideoInfo(id);
        if (videoLog == null || videoLog.getUrl() == null) {
            System.err.println("视频信息或路径为空");
            return -4;
        }
        String url = videoLog.getUrl();

        // 判断路径是否为本地文件
        if (url.startsWith("http")) {
            System.err.println("不支持删除网络地址文件: " + url);
            return -3;
        }
        File file = new File(url);
        if (file.exists()) {
            if (file.delete()) {
                luntaiTyreInstallLogMapper.updateDeleteInfo(id); // 确保只有删成功才更新数据库
                System.out.println("文件删除成功: " + url);
                return 1;
            } else {
                System.err.println("文件删除失败: " + url);
                return -1;
            }
        } else {
            System.err.println("文件不存在: " + url);
            return -2;
        }
    }

    @Override
    public int deleteTyreLog(Integer id) {
        return luntaiTyreInstallLogMapper.deleteLuntaiTyreInstallLogById(id);
    }


}
